package org.feerepository.client.components;

import java.util.ArrayList;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.PopupListener;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;


public class SearchPanel extends Composite {
	
	
	ToggleButton b1 ;
	
	PopupPanel pan = new PopupPanel(true);
	ArrayList<IndexedCheckBox> checkBoxes = new ArrayList<IndexedCheckBox>();
	ArrayList <IndexedLabel> lablels = new ArrayList<IndexedLabel>();
	
	private FlexTable displayList = new FlexTable();
	private ClickListener cbListener;
	private FlexTable cbDisplayList = new FlexTable();
	
	public SearchPanel(String title)
	{
		b1 = new ToggleButton(title);
		FlexCellFormatter formater = displayList.getFlexCellFormatter();
		displayList.setCellPadding(0);
		displayList.setCellSpacing(0);
		 cbListener = new ClickListener ()
			{
				public void onClick(Widget sender) 
				{
					IndexedCheckBox cb = (IndexedCheckBox)sender;
					refreshDisplay(cb.getIndex(),cb.isChecked());
				}
				
			};
			
			
		VerticalPanel content = new VerticalPanel();
		content.setWidth("100px");
		b1.setStylePrimaryName("my-ToggleButton");
	//	b1.setWidth("100px");
		b1.setHeight("20px");
		
		pan.setWidth("150px");
		pan.setWidget(cbDisplayList);
	//	pan.setHeight("100px");
		
		pan.addPopupListener(new PopupListener()
		{

			public void onPopupClosed(PopupPanel sender, boolean autoClosed)
			{

					if(autoClosed)
						b1.setDown(!b1.isDown());
				
			}
			
		});
		
		b1.addClickListener(new ClickListener()
		{

			public void onClick(Widget sender) 
			{
				ToggleButton owner = (ToggleButton)sender;
				if(owner.isDown() )
				{
					int left = sender.getAbsoluteLeft() ;
					int top = sender.getAbsoluteTop() + 20;
					pan.setPopupPosition(left, top);
					
					pan.show();
				}
				else
				{
					pan.hide();
			
				}
				
			}
			
		});
		content.add(b1);
		content.add(displayList);
		content.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		this.initWidget(content);
	}
	
	public void addItem(String name)
	{
		IndexedCheckBox cb = new IndexedCheckBox(name, this.checkBoxes.size());
		cb.addClickListener(cbListener);
		this.checkBoxes.add(cb);
		IndexedLabel label = new IndexedLabel(name, this.lablels.size());
		label.setOwnerPanel(this);
		this.lablels.add(label);
		cbDisplayList.setWidget(cb.getIndex(), 0, cb);
	}
	
	public void removeItem(int index)
	{
		this.checkBoxes.get(index).setChecked(false);
		refreshDisplay(index, false);
	}

	
	private void refreshDisplay(int index, boolean add)
	{
		if(!add)
		{
			displayList.remove(this.lablels.get(index));
		}
		else
			displayList.setWidget(index, 0, this.lablels.get(index));


	}
	
}
